package com.ztgf.pay.service;

import com.trz.netwk.api.system.TrdMessenger;
import com.trz.netwk.api.trd.TrdT1008Request;
import com.trz.netwk.api.trd.TrdT1008Response;
import com.trz.netwk.api.vo.BankInfo;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.pay.common.config.YmlConfig;
import com.ztgf.pay.models.dto.PayHttpLogDTO;
import com.ztgf.pay.models.service.IPayBankService;
import com.ztgf.pay.models.service.IPayHttpLogService;
import com.ztgf.pay.models.vo.PayBankVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DubboService
@Slf4j
public class PayBankServiceImpl implements IPayBankService {

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    private IPayHttpLogService httpLogService;

    @Override
    public Result<List<PayBankVO>> queryBankList(String bankType) {

        //添加请求日志
        PayHttpLogDTO logDTO = new PayHttpLogDTO();
        boolean httpSuccess = false;
        List<PayBankVO> list = new ArrayList<>();
        try {

            // 2. 实例化交易对象
            TrdT1008Request trdRequest = new TrdT1008Request();
            trdRequest.setMsghd_trdt(DateUtils.dateToStr(new Date(),DateUtils.DATE_FORMAT_DATESHORTDAY));
            trdRequest.setQryflag(bankType);//UDK 查询支持绑定该行结算 账户的银行列表 U3B 查询支持开通快捷支付 业务的银行列表 U6G 查询支持企业网银支付 的银行列表 U6P 查询支持个人网银支付 的银行列表 ALL 查询全部银行列表

            // 3. 报文处理
            trdRequest.process();
            logDTO.setParams(trdRequest.getRequestPlainText());
            System.out.println("请求报文[" + trdRequest.getRequestPlainText() + "]");
            System.out.println("签名原文[" + trdRequest.getRequestMessage() + "]");
            System.out.println("签名数据[" + trdRequest.getRequestSignature() + "]");

            // 4. 与融资平台通信
            TrdMessenger trdMessenger = new TrdMessenger();
            // message
            String respMsg = trdMessenger.send(trdRequest);

            // 5. 处理交易结果
            TrdT1008Response trdResponse = new TrdT1008Response(respMsg);
            System.out.println("响应报文[" + trdResponse.getResponsePlainText() + "]");
            logDTO.setReturnValue(trdResponse.getResponsePlainText());
            logDTO.setBusinessStatus(trdResponse.getMsghd_rspcode());
            logDTO.setBusinessNotes(trdResponse.getMsghd_rspmsg());
            logDTO.setParams(trdRequest.getRequestPlainText());
            logDTO.setStatus(0);

            // 交易成功 000000
            if ("000000".equals(trdResponse.getMsghd_rspcode())) {
                // ！！！ 在这里添加合作方处理逻辑！！！
                System.out.println("[msghd_rspmsg]=[" + trdResponse.getMsghd_rspmsg() + "]"); // 返回信息
                List<BankInfo> bankList = trdResponse.getBankList();
                System.out.println("===============================");
                for (BankInfo bankInfo : bankList) {
                    System.out.println("[bkid]=[" + bankInfo.getBkid() + "]"); // 银行编号(详见《银行编码.pdf》)
                    System.out.println("[bknm]=[" + bankInfo.getBknm() + "]"); // 银行名称
                    System.out.println("[sta]=[" + bankInfo.getSta() + "]"); // 业务开通状态(0未开通,1已开通,2已暂停)(QryFlag不等于ALL时有效)
                    System.out.println("[reason]=[" + bankInfo.getReason() + "]"); // 暂停原因(Sta=2时有效)
                    System.out.println("[zhbankid]=[" + bankInfo.getZhbankid() + "]"); // 总行编号
                    System.out.println("[busitype]=[" + bankInfo.getBusitype() + "]"); // 支持的业务类型

                    PayBankVO bankVO = new PayBankVO();
                    bankVO.setId(bankInfo.getBkid());
                    bankVO.setName(bankInfo.getBknm());
                    bankVO.setStatus(bankInfo.getSta());
                    bankVO.setHeadNo(bankInfo.getZhbankid());
                    bankVO.setBusinessType(bankInfo.getBusitype());
                    list.add(bankVO);
                }
                System.out.println("===============================");

                httpSuccess = true;


            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("TrdT1008Request is error", e);
            logDTO.setStatus(1);
            logDTO.setReturnValue("return 500");
        }
        //添加日志
//        logDTO.setId(ymlConfig.getPkId());
//        httpLogService.insert(logDTO);
        if(!httpSuccess){
            return new Result(CodeEnum.FAIL_200035);
        }
        return new Result(list);
    }
}
